/**
 * Created by new on 2017/5/23.
 */
$(document).ready(function () {

    $.jgrid.defaults.styleUI = 'Bootstrap';

    $("#table_list_2").jqGrid({
    	//data: mydata,
        url:'/admin/road/list',
        //编辑跳转此url
        editurl:'/admin/road/update',
    	datatype:"json",
        height: 425,
        mtype:"GET",
        autowidth: true,
        shrinkToFit: true,
        rowNum: 20,
        rowList: [10, 20, 30],
        colNames: ['序号', '走向', '道路权属', '道路名称', '起点名称', '终点名称', '所属街道', '养护单位', '道路等级', '横断面形式', '路况评定等级'],
        colModel: [
            {name: 'id', index: 'id', editable: true, width: 20, sortType: "int", search: true, align: "center",editoptions:{readonly:true},editurls:{custom:true,custom_func:idCheck}},
            {name: 'direction', index: 'direction', editable: true, width: 20, align: "center",editrules:{required:true}},
            {name: 'ownership', index: 'ownership', editable: true, width: 35, align: "center",editrules:{required:true}},
            {name: 'roadName', index: 'roadName', editable: true, width: 45, align: "center",editrules:{required:true}},
            {name: 'startName', index: 'startName', editable: true, width: 40, align: "center",editrules:{required:true}},
            {name: 'endName', index: 'endName', editable: true, width:40, align: "center", sortable: false,editrules:{required:true}},
            {name: 'street', index: 'street', editable: true, width: 50, align: "center",editrules:{required:true}},
            {name: 'maintenance', index: 'maintenance', editable: true, width: 60, align: "center",editrules:{required:true}},
            {name: 'roadLevel', index: 'roadLevel', editable: true, width: 25, align: "center",editrules:{required:true}},
            {name: 'transectType', index: 'transectType', editable: true, width: 25, align: "center",editrules:{required:true}},
            {name: 'assessLevel', index: 'assessLevel', editable: true, width: 30, align: "center"}
        ],
        pager: "#pager_list_2",
        altRows:true,
        viewrecords: true,
        cellEdit: false,
        treeGrid: false,
        caption: "道路数据",
        add: true,
        edit: true,
        addtext: 'Add',
        edittext: 'Edit',
        hidegrid: false,
        scroll: false,
        jsonReader: {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        }
    });

    function idCheck(value,id){
        if(value < 0 || value > 10000){
            return [false,"Please enter value between 0 and 9999"];
        }else {
            return [true,];
        }

    }


    // Add selection
    $("#table_list_2").setSelection(4, true);


    // Setup buttons
    $("#table_list_2").jqGrid('navGrid', '#pager_list_2', {
        edit: true,
        add: true,
        del: true,
        search: true,
        refresh:true
    }, {
        height: 200,
        reloadAfterSubmit: true
    //如果是添加就跳转此url
    }, {
        url:'/admin/road/save',
        closeAfterAdd: true
    //如果是删除就跳转此url
    }, {
        url:'/admin/road/delete',
        closeAfterDelete: true
    }, {
        url:'/admin/road/search',
        closeAfterSearch: true
    });

    // Add responsive to jqGrid
    $(window).bind('resize', function () {
        var width = $('.jqGrid_wrapper').width();
        $('#table_list_2').setGridWidth(width);
    });



});
// $.ajax({
//     url: '/admin/road/save',
//     type: 'POST',
//     datatype: "json",
//     contentType: "application/json;charset=UTF-8",
//     data: JSON.stringify(road),
//     success: function (Obj) {
//         if(Obj.code == 200){
//             console.log("执行成功！");
//             // $("#table_list_2").bind('resize');
//         }else {
//             console.log("执行失败");
//             alert("添加失败！");
//         }
//     }
//
// });

// $("#sData").click(function () {
//     $.ajax({
//         url: 'http://localhost:8088/admin/road/save',
//         type: 'POST',
//         data: JSON.stringify(data),
//         success: function (Obj) {
//             if(Obj.code == 200){
//                 console.log("执行成功！");
//                 $("#table_list_2").bind('resize');
//             }else {
//                 console.log("执行失败");
//                 $("#table_list_2").alert("添加失败！");
//             }
//         }
//
//     })
// })
/**
 * 添加商品
 */
// function addRoadInfo() {
//     window.location.href="/admin/road/save";
// }